home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SAMPLES / POINT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-19  |  4.7 KB  |  234 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <GL/glut.h>
  29.  
  30.  
  31. #define CI_RED COLOR_RED
  32. #define CI_ANTI_ALIAS_GREEN 16
  33. #define CI_ANTI_ALIAS_YELLOW 32
  34. #define CI_ANTI_ALIAS_RED 48
  35.  
  36.  
  37. GLenum rgb, doubleBuffer, windType;
  38. GLint windW, windH;
  39.  
  40. #include "tkmap.c"
  41.  
  42. GLenum mode;
  43. GLint size;
  44. float point[3] = {
  45.     1.0, 1.0, 0.0
  46. };
  47.  
  48.  
  49. static void Init(void)
  50. {
  51.     GLint i;
  52.  
  53.     glClearColor(0.0, 0.0, 0.0, 0.0);
  54.  
  55.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  56.  
  57.     if (!rgb) {
  58.     for (i = 0; i < 16; i++) {
  59.         glutSetColor(i+CI_ANTI_ALIAS_RED, i/15.0, 0.0, 0.0);
  60.         glutSetColor(i+CI_ANTI_ALIAS_YELLOW, i/15.0, i/15.0, 0.0);
  61.         glutSetColor(i+CI_ANTI_ALIAS_GREEN, 0.0, i/15.0, 0.0);
  62.     }
  63.     }
  64.  
  65.     mode = GL_FALSE;
  66.     size = 1;
  67. }
  68.  
  69. static void Reshape(int width, int height)
  70. {
  71.  
  72.     windW = (GLint)width;
  73.     windH = (GLint)height;
  74.  
  75.     glViewport(0, 0, width, height);
  76.  
  77.     glMatrixMode(GL_PROJECTION);
  78.     glLoadIdentity();
  79.     gluOrtho2D(-windW/2, windW/2, -windH/2, windH/2);
  80.     glMatrixMode(GL_MODELVIEW);
  81. }
  82.  
  83. static void Key2(int key, int x, int y)
  84. {
  85.  
  86.     switch (key) {
  87.       case GLUT_KEY_LEFT:
  88.     point[0] -= 0.25;
  89.     break;
  90.       case GLUT_KEY_RIGHT:
  91.     point[0] += 0.25;
  92.     break;
  93.       case GLUT_KEY_UP:
  94.     point[1] += 0.25;
  95.     break;
  96.       case GLUT_KEY_DOWN:
  97.     point[1] -= 0.25;
  98.     break;
  99.       default:
  100.     return;
  101.     }
  102.  
  103.     glutPostRedisplay();
  104. }
  105.  
  106. static void Key(unsigned char key, int x, int y)
  107. {
  108.  
  109.     switch (key) {
  110.       case 27:
  111.     exit(1);
  112.       case '1':
  113.     mode = !mode;
  114.     break;
  115.       case 'W':
  116.     size++;
  117.     break;
  118.       case 'w':
  119.     size--;
  120.     if (size < 1) {
  121.         size = 1;
  122.     }
  123.     break;
  124.       default:
  125.     return;
  126.     }
  127.  
  128.     glutPostRedisplay();
  129. }
  130.  
  131. static void Draw(void)
  132. {
  133.  
  134.     glClear(GL_COLOR_BUFFER_BIT);
  135.  
  136.     SetColor(COLOR_YELLOW);
  137.     glBegin(GL_LINE_STRIP);
  138.     glVertex2f(-windW/2, 0);
  139.     glVertex2f(windW/2, 0);
  140.     glEnd();
  141.     glBegin(GL_LINE_STRIP);
  142.     glVertex2f(0, -windH/2);
  143.     glVertex2f(0, windH/2);
  144.     glEnd();
  145.  
  146.     if (mode) {
  147.     glEnable(GL_BLEND);
  148.     glEnable(GL_POINT_SMOOTH);
  149.     } else {
  150.     glDisable(GL_BLEND);
  151.     glDisable(GL_POINT_SMOOTH);
  152.     }
  153.  
  154.     glPointSize(size);
  155.     if (mode) {
  156.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_ANTI_ALIAS_RED);
  157.     } else {
  158.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_RED);
  159.     }
  160.     glBegin(GL_POINTS);
  161.     glVertex3fv(point);
  162.     glEnd();
  163.  
  164.     glDisable(GL_POINT_SMOOTH);
  165.     glDisable(GL_BLEND);
  166.  
  167.     glPointSize(1);
  168.     SetColor(COLOR_GREEN);
  169.     glBegin(GL_POINTS);
  170.     glVertex3fv(point);
  171.     glEnd();
  172.  
  173.     glFlush();
  174.  
  175.     if (doubleBuffer) {
  176.     glutSwapBuffers();
  177.     }
  178. }
  179.  
  180. static GLenum Args(int argc, char **argv)
  181. {
  182.     GLint i;
  183.  
  184.     rgb = GL_TRUE;
  185.     doubleBuffer = GL_FALSE;
  186.  
  187.     for (i = 1; i < argc; i++) {
  188.     if (strcmp(argv[i], "-ci") == 0) {
  189.         rgb = GL_FALSE;
  190.     } else if (strcmp(argv[i], "-rgb") == 0) {
  191.         rgb = GL_TRUE;
  192.     } else if (strcmp(argv[i], "-sb") == 0) {
  193.         doubleBuffer = GL_FALSE;
  194.     } else if (strcmp(argv[i], "-db") == 0) {
  195.         doubleBuffer = GL_TRUE;
  196.     } else {
  197.         printf("%s (Bad option).\n", argv[i]);
  198.         return GL_FALSE;
  199.     }
  200.     }
  201.     return GL_TRUE;
  202. }
  203.  
  204. void main(int argc, char **argv)
  205. {
  206.     glutInit(&argc, argv);
  207.  
  208.     if (Args(argc, argv) == GL_FALSE) {
  209.     exit(1);
  210.     }
  211.  
  212.     windW = 300;
  213.     windH = 300;
  214.     glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH);
  215.  
  216.     windType = (rgb) ? GLUT_RGB : GLUT_INDEX;
  217.     windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  218.     glutInitDisplayMode(windType);
  219.  
  220.     if (glutCreateWindow("Point Test") == GL_FALSE) {
  221.     exit(1);
  222.     }
  223.  
  224.     InitMap();
  225.  
  226.     Init();
  227.  
  228.     glutReshapeFunc(Reshape);
  229.     glutKeyboardFunc(Key);
  230.     glutSpecialFunc(Key2);
  231.     glutDisplayFunc(Draw);
  232.     glutMainLoop();
  233. }
  234.